#!/bin/bash
# set -e
set -x
alias Fs="hadoop fs"
restart(){
    # assume you append the Hadoop ENVs to PATH
    stop-all.sh
    start-dfs.sh && start-yarn.sh
}
start(){
    start-dfs.sh && start-yarn.sh
}
clear_base(){
    Fs -rm -r -f output input
}
demo_base(){
    start
    mkdir -p ~/tmp/file
    echo "Hello World" > file/file1.txt
    echo "Hello Hadoop" > file/file2.txt
    Fs -mkdir -p input
    Fs -put ~/tmp/file/file* input/
}
pub_base(){
    declare jarpath
    if [ $1 ];then
        jarpath=$1
    else
        echo "Input your full example-jar path"
        read jarpath
    fi
    hadoop jar $jarpath wordcount input output
    Fs -cat output/part*
}
demo(){
    demo_base
    pub_base $1
    clear_base
}
work_base(){
    start
    # I think you can transfer the archieve file here
    unzip texts.zip -d ~/tmp/file
    rm -rf ~/tmp/file/__MACOSX
    mv ~/tmp/file/texts/* ~/tmp/file/
    Fs -mkdir -p input
    Fs -put ~/tmp/file/* input/
    # rename the file to HP1.txt HP2.txt HP3.txt manually
}
work(){
    work_base
    echo "unzip texts to ~/tmp/file "
    echo "Firstly, I think the zip path is './texts.zip'"
    echo "Then, I think you're smart enough to rename file to\
         HP1.txt HP2.txt HP3.txt iteratively"
    echo "yes or no?[press y to continue or others to exit]"
    read ans
    if [ $ans == "y" ];then
        pub_base $1
        clear_base
    else
        echo "I think you should do it before run this script"
    fi
}
echo "use source cmd for this file"
echo "[USAGE]: demo [jar_path]"
echo "[USAGE]: work [jar_path]"
echo "For example: demo ./exam.jar"